package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohm_time_op.entity.dto.MachineDTO;
import cn.lystudio.rohm_time_op.entity.dto.OpItemDTO;
import cn.lystudio.rohm_time_op.entity.po.OpItemPO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.OpItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/opItem")
@Slf4j
public class OpItemController {
    private OpItemService opItemService;
    @Lazy
    @Autowired
    public void setOpItemService(OpItemService opItemService) {
        this.opItemService = opItemService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit,@RequestParam(name = "keyword",defaultValue = "")String keyword){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<OpItemDTO> all = opItemService.getAll(keyword);
        if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer opItemID, Boolean enabled) {
        return SaResult.data(opItemService.uptOpItemEnabled(opItemID,enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delOpItem")
    public SaResult delOpItem(Integer opItemID){
        return SaResult.data(opItemService.delOpItem(opItemID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addOpItem")
    public SaResult addOpItem(@RequestBody OpItemPO opItemPO){
        if(ObjectUtil.isNotEmpty(opItemPO.getQrTypeID()) && opItemPO.getQrTypeID()<1) opItemPO.setQrTypeID(null);
        return  SaResult.data(opItemService.addOpItem(opItemPO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptOpItem")
    public SaResult uptOpItem(@RequestBody OpItemPO opItemPO){
        if(ObjectUtil.isNotEmpty(opItemPO.getQrTypeID()) && opItemPO.getQrTypeID()<1) opItemPO.setQrTypeID(null);
        return  SaResult.data(opItemService.uptOpItem(opItemPO));

    }

}
